/**
 * Created by yangfan on 2016/10/19.
 */
define(['config/appregister',
    "i18n",
    "business/services/httpServices",
    "business/careerPlan/services/httpServices",
    "business/home/services/filterService",
    "business/services/rightComponents",
    "business/services/filter"], function (app, i18n, httpService,carHttpservices) {
    app.controller("buildClassCtrl", function ($scope, $state,DetailFilter,$rootScope) {
        $scope.selectType = '所有格式';
        $scope.allType = "所有格式";
        //nav 区域设定
        $scope.showNavBox = false;
        $scope.navClass = "f-nav-out";

        //鼠标进入 改变导航区样式
        $scope.changeNav = function(curClass){
            if(curClass=$scope.navClass){
                $scope.navClass = "f-nav-enter";
            }
            $scope.showNavBox = true;
        };

        //鼠标移除 改变导航区样式
        $scope.mouseleave = function(mouseover){
            $scope.showNavBox = false;
            $scope.navClass = "f-nav-out"
        };

        //后台查询数据或缓存中查询数据。
        $scope.data = {
            "username":"Da Anna"
        };
        $scope.placeholder = "请输入您要输入的内容";
        $scope.mainData = {
            logoSrc:"them/images/gcj_big_logo.png"
        };
        $scope.isDetailPage = false;
        $scope.isMainpage = true;

        //图书馆
        $scope.i18n = i18n;
        $scope.curFileType = "";
        //点击登录
        $scope.login = function(){
            $state.go("login");
        };

        //详情列表参数
        $scope.listParams = {
            currentTcId: "",
            currentScId:"",
            download: false,
            time: true,
            pageNo: 1,
            pageSize: 10,
            all: false,
            typeId: "",
            scId: "",
            dcId: "",
            keyWord:"",
            //dcName scName 用于加入缓存 在详情中显示面包树导航
            dcName:"",
            scName:""
        };
        $scope.curIndex = 0;
        $scope.showBlueBg = {
            background:'url("them/images/compImages/build-bg-blue.png")no-repeat',
            color:"#fff"
        };
        $scope.showNormalBg = {
            background:'url("them/images/compImages/build-bg-white.png")no-repeat',
            color:"#30a3fe"
        };
        $scope.clickSec = function (index,item){
          $scope.curIndex  =  index ;
            $scope.listParams.scId = item.scId;
            var req = {
                type: "GET",
                data: {
                    scId: $scope.listParams.scId
                },
                uri: 'getDetailCategory'
            };
            getDetailCategory(req);
            //显示全部
            $scope.isShow.showDetailClassify = [];
            $scope.isShow.showDetailClassify[0] = true;
        };



        //后台加载的list数据
        $scope.list = [];
        function getListModel(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getThesisList();
                }
            };
        };



        //获取下载排行数据
        function getDownLoadRank() {
            var req = {
                type: "GET",
                count:10,
                data:{
                    mold:3
                },
                uri:"getDownLoadRank"
            };
            httpService.getDownLoadRank(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.downloadRank = response.data;
                getDownLoadRankConf($scope.downloadRank);
            }).error(function () {
            })
        }

        function getDownLoadRankConf(data){
            $scope.setConf = {
                data:data,
                idType:"fileId",
                state:"eng.techdetail"
            }
        }



        //获取热搜词
        function getKeyWord() {
            var req = {
                type: "GET",
                data:{
                    count:10
                },
                uri:"getKeyWord"
            };
            httpService.getKeyWord(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.keyWord = response.data;
            }).error(function () {
            })
        }


        getDownLoadRank();
        getKeyWord();


        /*
         * 论文 图书馆
         * */

        //getSecCategory
        function getSecCategory (){
            var req = {
                type:"GET",
                uri:"getBuildCategory",
                data:{
                    //tcId :$scope.listParams.tcId
                }
            };
            carHttpservices.buildService(req).success(function(response){
                if(!response.data) return ;
                $scope.$apply(function(){
                    $scope.libSecCategory = response.data;
                   // $scope.clickDataClassifyAll();
                    if( $scope.isJumpFromOther == true){
                       if($scope.curParams.hasOwnProperty("scId")){
                           $scope.listParams.scId = $scope.curParams.scId;
                       }
                    }else{
                        $scope.listParams.scId =  response.data[0].scId;
                    }
                     getDetailCategory();
                });
            }).error(function(

            ){})

        }
        function getHotFile(){
            var req = {
                type:"GET",
                uri:"getHotFile",
                data:{
                    scId:$scope.listParams.scId,
                    dcId:$scope.listParams.dcId
                }
            };
            carHttpservices.FileServices(req).success(function(response){
                $scope.hotFile = response.data;
            }).error(function(response){

            });
        }
        $scope.detailAll = {
            dcId:"",
            dcName:"全部",
            scId:$scope.listParams.scId,
            sequence:0
        };

        function getDetailCategory(){
            var req = {
                type:"GET",
                uri:"getDetailCategory",
                data:{
                    scId :$scope.listParams.scId,
                }
            };
            carHttpservices.buildService(req).success(function(response){
                if(!response.data) return ;
                $scope.$apply(function(){
                    $scope.libDateilCategory = response.data;

                    if( $scope.isJumpFromOther == true){
                        if($scope.curParams.hasOwnProperty("dcId")){
                            $scope.listParams.dcId = $scope.curParams.dcId;
                            $scope.isShow.showDetailClassify = [];
                            $scope.jumpFormOther = false;
                        }
                    }else{
                        $scope.listParams.dcId =  "";
                    }

                    getThesisList();
                    getHotFile();
                });
            }).error(function(

            ){})
        }

        //getThesis List
        function getThesisList(){
            var req = {
                type:"GET",
                uri:"getList",
                data:{
                    //tcId:$scope.listParams.tcId,
                    scId:$scope.listParams.scId,
                    dcId:$scope.listParams.dcId,
                    //typeId:$scope.listParams.typeId,
                    //keyWord:$scope.listParams.keyWord,
                    download:$scope.listParams.download,
                    time:$scope.listParams.time,
                    pagNo:1,
                    pageSize:10,
                    mold:3
                }
            };
            carHttpservices.getFileList(req).success(function(response){
                if(!response)return;
                $scope.$apply(function(){
                    $scope.ThesisList = response.data.list;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel();
                });
            }).error(function(response){ });
        }

        //getTyleList
        function getFileTypeList(){
            var req = {
                type:"GET"
            };
            httpService.getFileType(req).success(function(response){
                if(!response) return ;
                $scope.typeList = response.data;
            }).error(function(data){ })
        }

        //getBosterBarList
        function getHotPostbar(){
            var req = {
                type:"GET",
                data:{
                    userId:11,
                    pageNo:1,
                    pageSize:8
                },
                uri:"getHotPostbar"
            };
            carHttpservices.getHotPostbar(req).success(function(response){
                if(!response)return;
                $scope.$apply(function(){
                    $scope.bostbar = response.data;
                });
            }).error(function(response){});

        }
        function init (curParams){
            //判断从外部点击进入 JSON.parse(string)
            if(sessionStorage.hasOwnProperty("engParams") && ((toString.call(sessionStorage.engParams)=="[object Object]")||(toString.call(sessionStorage.engParams)=="[object String]"))&&sessionStorage.engParams!="") {
                if(toString.call(sessionStorage.engParams)=="[object Object]"){
                    $scope.curParams =  sessionStorage.engParams;
                }
                if(toString.call(sessionStorage.engParams)=="[object String]"){
                    $scope.curParams =  JSON.parse(sessionStorage.engParams);
                }
                sessionStorage.removeItem('engParams');
                $scope.isJumpFromOther = true;
                getSecCategory();
            }else{
                getSecCategory();
            }
        };


        //计算列表图片
        $scope.getPicType = function (typeId){
            if(!typeId) return ;
            if( typeId == 1){
                return "them/img/pdf.png"
            }
            else {
                return  "them/img/pdf.png"
            }
        };
        $scope.isJumpFromOther = false;
        init ();
        getFileTypeList();
        getHotPostbar();


        $scope.isShow = {
            showDataClassify: [],
            showFileType: [],
            //资料三级分类 显示flag
            showDetailClassify:[],
            showDetailBox:true,
            allUnderLine:false,
            allTypeUnderLine:true

        };
        $scope.listParams.typeId = null;
        //当前scId  和 dcId  只有在点击detail Category 之后才会确定 。
        $scope.clickDeatilCategory = function (detail,index) {
            if (!detail) {
                return
            }
            $scope.isShow.showDetailClassify = [];
            $scope.isShow.showDetailClassify[index] = true;
            $scope.listParams.scId = $scope.listParams.scId;
            $scope.listParams.dcId = detail.dcId;
            getHotFile();
            getThesisList();
        };



        // 点击搜索 搜索关键词
        $scope.searchKeyWord = function(){
            getThesisList();
        };

        //点击导航列表
        $scope.positionLeft = 0;
        // 初始化 建筑位置 设置
        $scope.isShow.showDataClassify[0] = true;
        $scope.isShow.showDetailClassify[0] = true;

        $scope.showA = function(index){
            $scope.isShow.showDataClassify = [];
            $scope.isShow.showDataClassify[index] = true;

        };
        $scope.mouseleave = function(index){
        };

        //点击时间 下载 按照这两种方式显示不同的图标 列表图标 图片图标
        var dropAndSort = {
            drop:"them/img/bottom.png",
            sort :"them/imag/top-blue-arrow.png"
        };
        $scope.picSrc = {
            theTime:dropAndSort.drop,
            download:dropAndSort.drop,
            list:"them/img/tablist.png",
            pic:"them/img/tabpic.png"
        };

        var changeTime = function(){
            if($scope.picSrc.theTime ==dropAndSort.drop){
                $scope.picSrc.theTime =dropAndSort.sort
            }else{
                $scope.picSrc.theTime =dropAndSort.drop
            }
        };
        var changeDownload = function(){
            if($scope.picSrc.download ==dropAndSort.drop){
                $scope.picSrc.download =dropAndSort.sort
            }else{
                $scope.picSrc.download =dropAndSort.drop
            }
        };

        //根据时间排序
        $scope.sortByTime = function () {
            changeTime();
            $scope.listParams.time = !$scope.listParams.time;
            if ($scope.listParams.time && $scope.listParams.download) {
                $scope.listParams.download = false;
            }
            getThesisList();
        };
        //根据下载排序
        $scope.sortByDownload = function () {
            changeDownload();
            $scope.listParams.download = !$scope.listParams.download;
            if ($scope.listParams.time && $scope.listParams.download) {
                $scope.listParams.time = false;
            }
            getThesisList();
        };
        //点击工程名 进入工程详情页
        $scope.enterEngDetail = function (item) {
            console.log(item)
            var state = {
                state:"eng.techdetail"
            };
            $state.go(state.state,{fileId:item.fileId},{reload:true});
        };
        $scope.stateGo =function(state,params){
            $state.go(state,params);
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.isInList = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
            $scope.isInList = true;
        };
        //热搜词跳转
        $scope.jumpToSearch = function(value){
            $rootScope.GLOBALKEYWORD =  value;
            sessionStorage.searchKeyWord = value;
            $state.transitionTo("search.data", {}, {
                reload: true, inherit: false, notify: true
            });
        }
        //选择文件格式 “全部” 以外 其他选项
        $scope.curFileType = "";
        $scope.checkFileType = function (item,index) {
            $scope.selectType=item.typeName;
            $scope.isShow.allTypeUnderLine = false;
            $scope.isShow.showFileType = [];
            $scope.isShow.showFileType[index] = true;
            $scope.listParams.typeId = item.typeId;
            getThesisList();
        };

    })
});

/**
 * Created by yangfan on 2016/12/15.
 */
